package com.wswy.wzcx.ui.fragment;

import android.app.Activity;
import android.content.Context;
import android.graphics.Rect;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.RecyclerView.ItemDecoration;
import android.support.v7.widget.RecyclerView.State;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.blankj.utilcode.util.SizeUtils;
import com.che.libcommon.api.ApiOptionalResultObserver;
import com.che.libcommon.api.BaseResult;
import com.che.libcommon.ui.VOMessage;
import com.che.libcommon.ui.loading.ILoadingView;
import com.che.libcommon.ui.loading.LoadingDelegate;
import com.che.libcommon.ui.loading.OnLoadingShowCallback;
import com.wswy.wzcx.R;
import com.wswy.wzcx.model.license.UserCarInfo;
import com.wswy.wzcx.module.UserDataProvider;
import com.wswy.wzcx.ui.adapter.MyCarCollectAdapter;
import com.wswy.wzcx.ui.persenter.MyCarCollectPresenter;
import com.wswy.wzcx.ui.view.MyCarCollectView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by wangduheng26 on 2018/1/19.
 * wangduheng26@gmail.com
 */

public class MyCarCollectFragment extends ILoginFragment implements MyCarCollectView {
    public static final String TAG = "MyCarCollectFragment";

    public static MyCarCollectFragment newInstance() {

        Bundle args = new Bundle();

        MyCarCollectFragment fragment = new MyCarCollectFragment();
        fragment.setArguments(args);
        return fragment;
    }

    private RecyclerView recyclerView;
    private List<UserCarInfo> list;
    private LoadingDelegate mLoadingDelegate;
    private MyCarCollectPresenter presenter;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        presenter = new MyCarCollectPresenter(this);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        final Context context = getContext();
        presenter.onCreate();

        View inflate = inflater.inflate(R.layout.fragment_my_cars, container, false);
        recyclerView = inflate.findViewById(R.id.recycler_view);
        mLoadingDelegate = new LoadingDelegate(inflate, R.id.layout_content, getLoadingConfig());
        return inflate;
    }

    private ILoadingView.LoadingConfigure getLoadingConfig() {
        return new ILoadingView.LoadingConfigure() {
            @Override
            public VOMessage getEmptyMessage() {
                return VOMessage.createEmpty(0,0,0);
            }

            @Override
            public View.OnClickListener getOnEmptyClickClickListener() {
                return null;
            }

            @Override
            public View.OnClickListener getOnRetryClickListener() {
                return null;
            }

            @Override
            public OnLoadingShowCallback getOnLoadingShowCallback() {
                return null;
            }
        };
    }

    MyCarCollectAdapter adapter;

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        Context context = getContext();
        list = new ArrayList<>();
        recyclerView.setLayoutManager(new LinearLayoutManager(context));
        recyclerView.setHasFixedSize(true);
        assert context != null;

        recyclerView.addItemDecoration(new ItemDecoration() {
            @Override
            public void getItemOffsets(Rect outRect, View view, RecyclerView parent, State state) {
                super.getItemOffsets(outRect, view, parent, state);
                int pos = parent.getChildAdapterPosition(view);
                if(pos == 0){
                    outRect.top = SizeUtils.dp2px(5);
                    outRect.bottom = 0;
                }else if(pos == state.getItemCount() -1){
                    outRect.top = 0;
                    outRect.bottom = SizeUtils.dp2px(5);
                }else {
                    outRect.top =outRect.bottom= SizeUtils.dp2px(3);
                }
            }
        });
        adapter = createAdapter();
        recyclerView.setAdapter(adapter);
    }



    private MyCarCollectAdapter createAdapter() {
        MyCarCollectAdapter adapter = new MyCarCollectAdapter(list);
        return adapter;
    }

    @Override
    public void setupPage() {
        UserDataProvider.get().myCars()
                .subscribe(new ApiOptionalResultObserver<List<UserCarInfo>>() {
                    @Override
                    protected void onApiResult(@Nullable List<UserCarInfo> infos) {
                        if (infos != null && !infos.isEmpty()) {
                            list.clear();
                            list.addAll(infos);
                            recyclerView.getAdapter().notifyDataSetChanged();
                            mLoadingDelegate.showLoading(false);
                            mLoadingDelegate.showData();
                        } else {
                            mLoadingDelegate.showError(VOMessage.createEmpty(R.drawable.mycar_empty_default,R.string.empty_cars,0));
                        }

                    }

                    @Override
                    protected void onApiError(@Nullable BaseResult result) {
                        toastErrorMsg(result);
                    }

                    @Override
                    protected void onStart() {
                        super.onStart();
                        mLoadingDelegate.showLoading(true);
                    }
                });


    }

    @Override
    public String getFragmentTitle() {
        return "我的车库";
    }




    @Override
    public Activity activity() {
        return getActivity();
    }

    @Override
    public void updateItem(UserCarInfo userCarInfo) {
        if (adapter != null) {
            adapter.updateItem(userCarInfo);
        }
    }

    @Override
    public void deleteItem(int carId) {
        if (adapter != null) {
            adapter.deletcItem(carId);
        }
    }
}
